<!-- 绿色通道 - 患者列表 - 医疗记录 - 基本信息(旧) 界面 -->
<template>
  <div class="info">
    <el-row type="flex" justify="end" style="padding-right: 40px">
      <el-button type="primary" @click="update">保存</el-button>
    </el-row>
    <el-form ref="patientForm" :model="patientForm" :rules="rules" label-width="128px" class="pageForm">
      <el-row>
        <el-col :span="24">
          <h3 class="fl">读卡信息</h3>
        </el-col>
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="患者姓名" prop="name">
              <el-input v-model="patientForm.name" size="mini" />
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="4">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="patientForm.sex" size="mini" clearable>
                <el-option value="">请选择</el-option>
                <el-option value="男" />
                <el-option value="女" />
                <el-option value="未知" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="年龄" prop="age">
              <el-input v-model.number="patientForm.age" size="mini" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="4">
            <el-form-item label="民族" prop="ethnicity">
              <el-select v-model="patientForm.ethnicity" size="mini" filterable clearable>
                <el-option v-for="(nation, index) in nationList" :key="'民族' + index" :value="nation.nationName" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="身份证号码/卡号" prop="idcardidCopy">
              <el-input v-model="patientForm.idcardidCopy" size="mini" @change="changeIdcard" />
            </el-form-item>
          </el-col>
        </el-col>

        <el-col :span="24">
          <h3 class="fl">补充信息</h3>
        </el-col>
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="患者电话">
              <el-input v-model="patientForm.contactphone" size="mini" />
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="4">
            <el-form-item label="联系人姓名" prop="familyName">
              <el-input v-model="patientForm.familyName" size="mini" />
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="联系人电话">
              <el-input v-model="patientForm.familyPhone" size="mini" />
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="4">
            <el-form-item label="与患者关系">
              <el-input v-model="patientForm.familyRelation" size="mini" />
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="住院ID" prop="inpatientId">
              <el-input v-model="patientForm.inpatientId" size="mini" />
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="4">
            <el-form-item label="医保类型" prop="medicalinsurancetype">
              <el-select v-model="patientForm.medicalinsurancetype" size="mini" clearable>
                <el-option label="" value="">请选择</el-option>
                <el-option label="城镇职工基本医疗保险" value="01" />
                <el-option label="城镇居民基本医疗保险" value="02" />
                <el-option label="新型农村合作医疗" value="03" />
                <el-option label="贫困救助" value="04" />
                <el-option label="商业医疗保险" value="05" />
                <el-option label="全公费" value="06" />
                <el-option label="全自费" value="07" />
                <el-option label="其他社会保险" value="08" />
                <el-option label="其他" value="99" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24" />
      </el-row>
      <div class="line" />
      <div style="margin-bottom: 10px;text-align: right;width: 98%;margin: 0px auto;">
        <el-button type="primary" size="small" @click="UploadImg(url)">上传图片</el-button>
      </div>
      <el-row v-if="url.length > 0" type="flex" style="margin-top: 10px">
        <div v-for="(item, index) in url" :key="'图片' + index" class="outerBox">
          <i class="el-icon-error icon-style" @click="deleteImg(item)" />
          <el-image
            style="width: 100px; height: 100px; margin-left: 60px"
            :src="item"
            :preview-src-list="srcList"
            @click.stop="handleClick"
          />
        </div>
      </el-row>
      <el-row v-if="url.length == 0">
        <div
          style="width: 100px; height: 100px; margin-left: 60px; border: 1px solid #ddd; color: #ddd; display: flex; justify-content: center; align-items: center;"
        >
          无图片
        </div>
      </el-row>
      <div class="line" />
    </el-form>
    <!-- 上传图片弹窗 -->
    <el-dialog title="上传图片" :visible.sync="dialogVisible" width="60%" :before-close="handleClose">
      <el-upload
        ref="date"
        action=""
        list-type="picture-card"
        :limit="10"
        :file-list="fileList"
        :auto-upload="false"
        :on-exceed="handleExceed"
        :on-change="uploadChange"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
      >
        <i class="el-icon-plus" />
      </el-upload>
      <el-dialog append-to-body width="50vh" top="50px" :visible.sync="dialogVisibleCard">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitUpload()">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  selectRegionEx,
  getPatientStroke,
  savePatientInfo,
  getPatientsImg,
  savePatientImg
} from '@/api/green-channel/info'

export default {
  // 由主页面(父组件) 接收来的, 选择项的数据
  // eslint-disable-next-line vue/require-prop-types
  props: ['checkrowdata'], // 由主页面(父组件) 接收来的, 选择项的数据
  data() {
    return {
      areaList: [],
      persondata: {},
      url: [],
      patientForm: {
        idcardidCopy: ''
      },
      srcList: [],
      nationList: [{
        nationCode: '01',
        nationName: '汉族'
      },
      {
        nationCode: '02',
        nationName: '蒙古族'
      },
      {
        nationCode: '03',
        nationName: '回族'
      },
      {
        nationCode: '04',
        nationName: '藏族'
      },
      {
        nationCode: '05',
        nationName: '维吾尔族'
      },
      {
        nationCode: '06',
        nationName: '苗族'
      },
      {
        nationCode: '07',
        nationName: '彝族'
      },
      {
        nationCode: '08',
        nationName: '壮族'
      },
      {
        nationCode: '09',
        nationName: '布依族'
      },
      {
        nationCode: '10',
        nationName: '朝鲜族'
      },
      {
        nationCode: '11',
        nationName: '满族'
      },
      {
        nationCode: '12',
        nationName: '侗族'
      },
      {
        nationCode: '13',
        nationName: '瑶族'
      },
      {
        nationCode: '14',
        nationName: '白族'
      },
      {
        nationCode: '15',
        nationName: '土家族'
      },
      {
        nationCode: '16',
        nationName: '哈尼族'
      },
      {
        nationCode: '17',
        nationName: '哈萨克族'
      },
      {
        nationCode: '18',
        nationName: '傣族'
      },
      {
        nationCode: '19',
        nationName: '黎族'
      },
      {
        nationCode: '20',
        nationName: '傈僳族'
      },
      {
        nationCode: '21',
        nationName: '佤族'
      },
      {
        nationCode: '22',
        nationName: '拉祜族'
      },
      {
        nationCode: '23',
        nationName: '高山族'
      },
      {
        nationCode: '24',
        nationName: '白族'
      },
      {
        nationCode: '25',
        nationName: '水族'
      },
      {
        nationCode: '26',
        nationName: '东乡族'
      },
      {
        nationCode: '27',
        nationName: '纳西族'
      },
      {
        nationCode: '28',
        nationName: '景颇族'
      },
      {
        nationCode: '29',
        nationName: '柯尔克孜族'
      },
      {
        nationCode: '30',
        nationName: '土族'
      },
      {
        nationCode: '31',
        nationName: '达斡尔族'
      },
      {
        nationCode: '32',
        nationName: '仫佬族'
      },
      {
        nationCode: '33',
        nationName: '羌族'
      },
      {
        nationCode: '34',
        nationName: '布朗族'
      },
      {
        nationCode: '35',
        nationName: '撒拉族'
      },
      {
        nationCode: '36',
        nationName: '毛南族'
      },
      {
        nationCode: '37',
        nationName: '仡佬族'
      },
      {
        nationCode: '38',
        nationName: '锡伯族'
      },
      {
        nationCode: '39',
        nationName: '阿昌族'
      },
      {
        nationCode: '40',
        nationName: '土族普米族'
      },
      {
        nationCode: '41',
        nationName: '塔吉克族'
      },
      {
        nationCode: '42',
        nationName: '怒族'
      },
      {
        nationCode: '43',
        nationName: '乌孜别克族'
      },
      {
        nationCode: '44',
        nationName: '俄罗斯族'
      },
      {
        nationCode: '45',
        nationName: '鄂温克族'
      },
      {
        nationCode: '46',
        nationName: '崩龙族'
      },
      {
        nationCode: '47',
        nationName: '保安族'
      },
      {
        nationCode: '48',
        nationName: '裕固族'
      },
      {
        nationCode: '49',
        nationName: '京族'
      },
      {
        nationCode: '50',
        nationName: '塔塔尔族'
      },
      {
        nationCode: '51',
        nationName: '独龙族'
      },
      {
        nationCode: '52',
        nationName: '鄂伦春族'
      },
      {
        nationCode: '53',
        nationName: '赫哲族'
      },
      {
        nationCode: '54',
        nationName: '门巴族'
      },
      {
        nationCode: '55',
        nationName: '珞巴族'
      },
      {
        nationCode: '56',
        nationName: '基诺族'
      },
      {
        nationCode: '97',
        nationName: '其他'
      },
      {
        nationCode: '98',
        nationName: '外国血统'
      }],
      deptList: [],

      /* 上传图片  */
      dialogVisible: false,
      dialogVisibleCard: false,
      dialogImageUrl: '',
      fileList: [],
      rules: {
        incidentProvince: [{
          required: true,
          message: '请选择发病地址',
          trigger: 'blur'
        }],
        incidentDateTime: [{
          required: true,
          message: '请选择发病时间',
          trigger: 'blur'
        }],
        name: [{
          required: true,
          message: '请输入姓名',
          trigger: 'blur'
        }],
        sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
        familyName: [{
          required: true,
          message: '请输入联系人姓名',
          trigger: 'blur'
        }],
        inpatientId: [{
          required: true,
          message: '请输入住院ID',
          trigger: 'blur'
        }],
        medicalinsurancetype: [{
          required: true,
          message: '请选择医保类型',
          trigger: 'blur'
        }],
        age: [{
          required: true,
          message: '请输入年龄',
          trigger: 'blur'
        }],
        idcardidCopy: [{
          required: true,
          message: '请输入身份证号/卡号',
          trigger: 'blur'
        }],
        credentialstype: [{
          required: true,
          message: '请选择证件类型',
          trigger: 'blur'
        }],
        helpDate: [{
          required: true,
          message: '请选择呼救时间',
          trigger: 'blur'
        }],
        cwZyTransType: [{
          required: true,
          message: '请选择转院类型',
          trigger: 'blur'
        }],
        ethnicity: [{
          required: true,
          message: '请选择民族类型',
          trigger: 'blur'
        }]
      }
    }
  },
  created() {
    this.getImg()
    this.getPatientInfo()
    this.selectRegionEx()
  },
  methods: {
    changeIdcard(value) {
      if (value) {
        this.patientForm.age = this.getAge(value)
        this.patientForm.sex = this.getSex(value)
      } else {
        this.patientForm.age = ''
        this.patientForm.sex = ''
      }
    },
    getSex(idcard) {
      if (!idcard) {
        return ''
      }
      const len = (idcard + '').length
      if (len == 18) {
        if (idcard.charAt(16) % 2 == 0) {
          return '女'
        } else {
          return '男'
        }
      } else if (len == 15) {
        if (idcard.charAt(14) % 2 == 0) {
          return '女'
        } else {
          return '男'
        }
      } else {
        return ''
      }
    },
    getAge(idcard) {
      if (!idcard) {
        return ''
      }
      const len = (idcard + '').length
      let strBirthday = ''
      if (len == 18) {
        strBirthday = idcard.substr(6, 4) + '/' + idcard.substr(10, 2) + '/' + idcard.substr(12, 2)
      } else if (len == 15) {
        let birthdayValue = ''
        birthdayValue = idcard.charAt(6) + idcard.charAt(7)
        if (parseInt(birthdayValue) < 10) {
          strBirthday = '20' + idcard.substr(6, 2) + '/' + idcard.substr(8, 2) + '/' + idcard.substr(10, 2)
        } else {
          strBirthday = '19' + idcard.substr(6, 2) + '/' + idcard.substr(8, 2) + '/' + idcard.substr(10, 2)
        }
      } else {
        strBirthday = ''
      }
      if (strBirthday) {
        const birthDate = new Date(strBirthday)
        const nowDateTime = new Date()
        let age = nowDateTime.getFullYear() - birthDate.getFullYear()
        if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
          age--
        }
        return age
      } else {
        return ''
      }
    },
    handleClick() {
      this.$nextTick(() => {
        const div = document.getElementsByClassName('el-image-viewer__actions__inner')
        const i = document.createElement('i')
        i.className = 'close-icon el-icon-close'
        document.querySelector('.el-image-viewer__close').style.display = 'none'
        div[0].appendChild(i)
        i.addEventListener('click', () => {
          document.querySelector('.el-image-viewer__close').click()
        })
      })
    },
    getImg() {
      getPatientsImg(this.checkrowdata.patientId).then((res) => {
        if (res.data) {
          this.srcList = res.data.split(',')
          this.url = res.data.split(',')
        }
      })
    },
    update() {
      let whether = true
      this.patientForm.patientId = this.checkrowdata.patientId
      this.patientForm.dispatchId = this.checkrowdata.dispatchId
      if (this.patientForm.idcardidCopy && this.patientForm.idcardidCopy.indexOf('*') == -1) {
        this.patientForm.idcardid = this.patientForm.idcardidCopy
      }
      if (!this.patientForm.name) {
        whether = false
        this.$message.warning('姓名必须填写!')
      } else if (!this.patientForm.sex) {
        whether = false
        this.$message.warning('性别必须填写!')
      } else if (!this.patientForm.age) {
        whether = false
        this.$message.warning('年龄必须填写!')
      } else if (!this.patientForm.idcardid) {
        whether = false
        this.$message.warning('身份证号码/卡号必须填写!')
      } else if (!this.patientForm.ethnicity) {
        whether = false
        this.$message.warning('民族必须填写!')
      }else if (!this.patientForm.familyName) {
        whether = false
        this.$message.warning('联系人姓名必须填写!')
      }else if (!this.patientForm.inpatientId) {
        whether = false
        this.$message.warning('住院ID必须填写!')
      }else if (!this.patientForm.medicalinsurancetype) {
        whether = false
        this.$message.warning('医保类型必须填写!')
      }

      if (whether) {
        savePatientInfo(this.patientForm).then((res) => {
          if (res.code === 0) {
            this.$message.success('保存成功')
            this.getPatientInfo()
          } else {
            this.$message.error('保存失败')
          }
        })
      }
    },
    selectRegionEx() {
      selectRegionEx().then((res) => {
        this.areaList = res.data
      })
    },
    BMI(weight, height) {
      let num = 0
      if (weight && height) {
        num = weight / ((height * height) / 10000)
      }
      if (num === 0) {
        this.patientForm.BMI = null
      } else {
        this.patientForm.BMI = num.toFixed(1)
      }
    },
    getPatientInfo() {
      getPatientStroke(
        this.checkrowdata.patientId,
        this.checkrowdata.dispatchId
      ).then((res) => {
        this.patientForm = res.data
        if (res.data.idcardid) {
          this.$set(this.patientForm, 'idcardidCopy', res.data.idcardid.replace(/^(.{6})(?:\w+)(.{4})$/, '\$1******\$2'))
        }
      })
    },
    /* 上传图片 --------- */
    UploadImg(url) {
      const urlList = url.map((item) => {
        return {
          name: item,
          url: item
        }
      })
      this.fileList = urlList
      this.dialogVisible = true
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.fileList = []
        })
        .catch(_ => { })
    },
    cancel() {
      this.dialogVisible = false
      this.fileList = []
    },
    handleExceed(files, fileList) {
      this.$message.warning(`最多可上传十张照片`)
    },
    uploadChange(file, fileList) {
      this.fileList = fileList
    },
    handleRemove(file, fileList) {
      this.fileList = fileList
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisibleCard = true
    },
    /* 删除图片 */
    deleteImg(item) {
      this.$confirm('确认删除本张图片？')
        .then(_ => {
          if (this.url.includes(item)) {
            this.url.splice(item, 1)
            console.log('url', this.url)
          }
          const formData = new FormData()
          const imageUrl = this.url.join(',')
          formData.set('imageUrl', imageUrl)
          formData.append('patientId', this.checkrowdata.patientId)
          savePatientImg(formData).then((res) => {
            console.log(res)
            if (res.code === 0) {
              this.$message.success('删除成功')
              this.dialogVisible = false
              this.fileList = []
              this.getImg()
            }
          }).catch(() => {
            this.$message.error('删除失败')
          })
          // done()
        })
        .catch(_ => { })
    },
    // 保存
    submitUpload() {
      const formData = new FormData()
      const imageUrlArr = []
      let imageUrl = ''
      formData.append('patientId', this.checkrowdata.patientId)
      console.log(this.fileList)
      this.fileList.forEach((item, key) => {
        if (item.status === 'success') {
          imageUrlArr.push(item.url)
          imageUrl = imageUrlArr.join(',')
          formData.set('imageUrl', imageUrl)
        } else {
          formData.append('image' + (key + 1), item.raw)
        }
      })
      savePatientImg(formData).then((res) => {
        console.log(res)
        if (res.code === 0) {
          this.$message.success('保存成功')
          this.dialogVisible = false
          this.fileList = []
          this.getImg()
        }
      }).catch(() => {
        this.$message.error('保存失败')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.info {
  overflow-x: auto;
  min-height: 80vh;

  .equal-row-height {
    line-height: 36px;
  }

  .form-row {
    .equal-row-height {
      margin-left: 20px;
    }
  }

  // .el-form-item {
  //   margin-bottom: 10px;
  // }

  .in-hospital {
    .title {
      width: 128px;
      font-size: 14px;
      color: #606266;
      text-align: right;
      padding-right: 12px;
      line-height: 36px;
    }
  }

  .line {
    height: 1px;
    width: 100%;
    border-top: 1px dashed #dcdfe6;
    margin: 10px 0 9px 0;
  }

  .outerBox {
    position: relative;
  }

  .icon-style {
    position: absolute;
    top: 0px;
    right: 0px;
    z-index: 99;
    font-size: 20px;
    font-weight: 700;
  }

  .icon-style:hover {
    cursor: pointer;
  }
}
</style>
<style lang="scss">
.info {
  .el-form-item__label {
    font-weight: normal;
  }

  .not-accurate-minutes {
    .el-input__inner {
      width: 130px;
      min-width: unset;
    }
  }

  .short-text {
    .el-input__inner {
      width: 85px;
      min-width: unset;
    }
  }

  .el-input__inner {
    min-width: 220px;
  }

  .radio-div {
    min-width: 220px;
  }

  .long-text {
    .el-input__inner {
      min-width: 545px;
    }
  }
}
</style>

<style scoped>
.table-bordered {
  border: 1px solid #ddd;
}

.table {
  width: 100%;
  max-width: 100%;
  margin-bottom: 20px;
}

th,
td {
  padding: 5px;
  border: 1px solid #ddd;
}

th {
  width: 200px;
}

td {
  width: 500px;
}

.pageForm {
  padding: 0 30px;
}
</style>
